//package kafka.client.producer;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.UnsupportedEncodingException;
//import java.util.Properties;
//
//import org.apache.commons.lang3.StringUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//
//import com.bj58.spat.esbclient.ESBClient;
//import com.bj58.spat.esbclient.ESBMessage;
//import com.bj58.spat.esbclient.ESBReceiveHandler;
//import com.bj58.spat.esbclient.ESBSubject;
//
///**
// * @author zhaoxiang
// * 2015-07-10
// */
//public class IMCProducer_SyncPool {
//	private static final Logger logger = LoggerFactory.getLogger(IMCProducer_SyncPool.class);
//	private MessageSenderPool pool ;
//	private ESBClient client;
//	private String TOPIC;
//	private MessageEncoder<String> stringEncoder;
//	
//	private IMCProducer_SyncPool(){
//		initProducer();
//		registerExitEvent();
////		initESBClient();
//	}
//	
//	public static final IMCProducer_SyncPool getInstance() {  
//		 return SingletonHolder.INSTANCE;  
//	} 
//	
//	/*
//	 * 加载配置文件，初始化Producer
//	 */
//	private  void initProducer() {
//		try {
//			InputStream inStream = IMCProducer_SyncPool.class.getResourceAsStream("/kafka-producer-conf.properties");
//			Properties props = new Properties();
//			props.load(inStream);
//			this.TOPIC = props.getProperty("topic");
//			props.remove("topic");
//			pool = new MessageSenderPool(Runtime.getRuntime().availableProcessors()+1, TOPIC, props);
//			stringEncoder = new MessageEncoder<String>() {
//				@Override
//				public byte[] encode(String msg) {
//					byte[] ret = null;
//					try {
//						ret = msg.getBytes("UTF-8");
//					} catch (UnsupportedEncodingException e) {
//						e.printStackTrace();
//					}
//					return ret;
//				}
//			};
//		} catch (IOException e) {
//			logger.error("ERROR Occurs when init the IMCProducer: \n",e);
//			System.exit(1);
//		}
//	}
//
//	/*
//	 * 添加回调方法，关闭producer
//	 */
//    private  void registerExitEvent()
//    {
//        Runtime.getRuntime().addShutdownHook(new Thread() {
//	            public void run() {
//	            	if(pool !=null)
//	            		pool.close();
//	            }
//        	}
//        );
//    }
//    
//	/*
//	 * 加载配置文件，初始化 ESBClient
//	 */
//	private  void initESBClient() {
//		try {
//			InputStream inStream = IMCProducer_SyncPool.class.getResourceAsStream("/esb-conf.properties");
//			Properties props = new Properties();
//			props.load(inStream);
//			inStream.close();
//			client = new ESBClient(props.getProperty("url"));
//			client.setReceiveSubject(new ESBSubject(Integer.parseInt(props.getProperty("subjectId"))));
//			client.setReceiveHandler(new ESBReceiveHandler() {
//				@Override
//				public void messageReceived(ESBMessage msg) {
//					// 使用线程池，及时处理请求
//		            String data = String.valueOf(msg.getBody());
//		            MessageSender sender = pool.getSender(2000);
//		            sender.send(data,stringEncoder);
//		            sender.close();
//				}
//			});
//			// 保持监听状态，让ESB主线程不消失
//			while (true) {
//				Thread.sleep(1000 * 60 * 60);
//			}
//		} catch (Exception e) {
//			logger.error("ERROR Occurs : ",e);
//		}finally{
//			System.exit(1);
//		}
//	}
//		
//	protected void start() {
//		try {
//			if(StringUtils.isBlank(TOPIC)){
//				logger.error("TOPIC is null.");
//				System.exit(1);
//			}
//			
//			initESBClient();
//			
////	        int messageNo = 1;
////	        final int COUNT = 100;
////	 
////	        while (messageNo <= COUNT) {
////	            String key = String.valueOf(messageNo);
////	            String data = "hello kafka message: " + key;
////	            try {
////		            MessageSender sender = pool.getSender(2000);
////		            sender.send(data,stringEncoder);
////		            sender.close();
////	            } catch (Exception e) {
////	    			logger.error("ERROR Occurs when send a message: "+key,e);
////	    		}
////	            System.out.println(data);
////	            messageNo ++;
////	        }
//		} catch (Exception e) {
//			logger.error("ERROR Occurs in function start() program will exit: ",e);
//		}finally{
//			System.exit(1);
//		}
//    }
//	
//	private static class SingletonHolder {  
//		private static final IMCProducer_SyncPool INSTANCE = new IMCProducer_SyncPool();  
//	} 
//	
//	/**
//	 * @param args
//	 */
////	public static void main(String[] args) {
////		IMCProducer_SyncPool producer = IMCProducer_SyncPool.getInstance();
////		producer.start();
////	}
//
//}
